<?php
namespace app\common\component\user_vip;

use think\Session;
use app\common\component\user_wallet_vip\UserWalletVipService;
use app\common\component\user_wallet_vip_log\UserWalletVipLogService;
use app\common\component\user_wallet_silver\UserWalletSilverService;

class UserVipService {
	public function getUserVip($user_id = null) {
		if(!$user_id) {
			$user_id = Session::get('user_id');
		}

		$getUserVip = UserVipModel::get($user_id);
		if(!$getUserVip) {
			$resultUserVip = $this -> create([
				'user_id' => $user_id,
				'level' => 0,
				'days' => 0,
				'top_up_time' => 0
			]);

			if($resultUserVip['code'] === 0) {
				$getUserVip = $resultUserVip['data']['user_vip'];
			} else {
				$getUserVip = null;
			}
		}

		if($getUserVip) {
			return [
				'code' => 0,
				'data' => [
					'user_vip' => $getUserVip
				]
			];
		}

		return [
			'code' => -1,
			'message' => '失败'
		];
	}
	
	public function create($data) {
		$userVipModel = new UserVipModel($data);

    	if($userVipModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '创建成功',
    			'data' => [
    				'user_vip' => $userVipModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '创建失败'
		];
	}

	public function update($data, $user_id = null) {
    	$userVipModel = new UserVipModel();

    	if($user_id && $user_id > 0) {
    		$userVipModel = $userVipModel -> where('user_id', $user_id) -> update($data);
    	} else if(Session::has('user_id')) {
    		$userVipModel = $userVipModel -> where('user_id', Session::get('user_id')) -> update($data);
    	} else {
    		$userVipModel = false;
    	}

    	if($userVipModel) {
    		return [
    			'code' => 0,
    			'message' => '修改成功'
    		];
    	}

    	return [
			'code' => -1,
			'message' => '修改失败'
		];
    }

	/**
	 * 到期续费或初始化为0
	 */
	public function clearVip($user_id = null) {
		$resultUserVip = $this -> getUserVip($user_id);
		$resultUserVip['data']['user_vip'] -> countRemainingDays();

		if($resultUserVip['data']['user_vip'] -> remaining_days <= 0) {
			$VIP_EVERY_MONTH_MONEY = getParam('VIP_EVERY_MONTH_MONEY');

			$resultUserWalletVip = (new UserWalletVipService()) -> getUserWalletVipV01($resultUserVip['data']['user_vip'] -> user_id);
			if($resultUserWalletVip['data']['user_wallet_vip'] -> price >= $VIP_EVERY_MONTH_MONEY) {
				$currentTime = time();
				\think\Db::startTrans();
				try {
					if(!$resultUserWalletVip['data']['user_wallet_vip'] -> setDec('price', $VIP_EVERY_MONTH_MONEY)) {
						throw new \Exception('user_id:' . $resultUserVip['data']['user_vip'] -> user_id . ', 扣款失败!');
					}

					$days = floor((strtotime('+1 month') - $currentTime) / 24 / 60 / 60);

					$resultUserVip['data']['user_vip'] -> days = $days;
					$resultUserVip['data']['user_vip'] -> top_up_time = $currentTime;
					if(!$resultUserVip['data']['user_vip'] -> save()) {
						throw new \Exception('user_id:' . $resultUserVip['data']['user_vip'] -> user_id . ', vip修改失败!');
					}

					$resultUserWalletVipLog = (new UserWalletVipLogService()) -> createLogV01(2, 0, $VIP_EVERY_MONTH_MONEY, 1);
					if($resultUserWalletVipLog['code'] != 0) {
						throw new \Exception('user_id:' . $resultUserVip['data']['user_vip'] -> user_id . ', ' . $resultUserWalletVipLog['message'] . '!');
					}

					$resultUserWalletSilver = (new UserWalletSilverService()) -> createLogV01($resultUserVip['data']['user_vip'] -> user_id, 2, 3);
					if($resultUserWalletSilver['code'] != 0) {
						throw new \Exception('user_id:' . $resultUserVip['data']['user_vip'] -> user_id . ', ' . $resultUserWalletSilver['message'] . '!');
					}
					
					\think\Db::commit();
				} catch(\Exception $e) {
					\think\Db::rollback();

					\think\Log::error($e -> getMessage());
				}

			} else {
				$resultUserVip['data']['user_vip'] -> days = 0;
				$resultUserVip['data']['user_vip'] -> top_up_time = 0;

				$resultUserVip['data']['user_vip'] -> save();
				if(!$resultUserVip['data']['user_vip'] -> save()) {
					\think\Log::error('user_id:' . $resultUserVip['data']['user_vip'] -> user_id . ', vip修改失败!');
				}
			}
		}
		return $resultUserVip;
	}
}
?>